package com.service.impl;

import com.dao.UserDao;
import com.dao.impl.UserDaoImpl;
import com.pojo.User;
import com.service.UserService;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();

    @Override
    public User getUser(User user) {
        ResultSet userOne = userDao.getUserOne(user);
        User user1 =null;
        try {
            while (userOne.next()){
                user1=new User();
                user1.setId(userOne.getInt("id"));
                user1.setName(userOne.getString("uname"));
                user1.setPassword(userOne.getString("upassword"));
                user1.setAge(userOne.getInt("age"));
                user1.setSex(userOne.getInt("sex"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return user1;
    }

    @Override
    public User getUser(int id) {
        ResultSet userOne = userDao.getUserOne(id);
        User user1 =null;
        try {
            while (userOne.next()){
                user1=new User();
                user1.setId(userOne.getInt("id"));
                user1.setName(userOne.getString("uname"));
                user1.setPassword(userOne.getString("upassword"));
                user1.setAge(userOne.getInt("age"));
                user1.setSex(userOne.getInt("sex"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return user1;
    }

    @Override
    public List<User> getUserAll() {
        ResultSet userOne = userDao.getUserAll();
       List<User> userList = new ArrayList<User>();
        try {
            while (userOne.next()){
                User user1 =new User();
                user1.setId(userOne.getInt("id"));
                user1.setName(userOne.getString("uname"));
                user1.setPassword(userOne.getString("upassword"));
                user1.setAge(userOne.getInt("age"));
                user1.setSex(userOne.getInt("sex"));
                userList.add(user1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public List<User> getUserAll(int page, int limit) {
        //处理分页参数，
        int cuur=(page-1)*limit;
        ResultSet userOne = userDao.getUserAll(cuur,limit);
        List<User> userList = new ArrayList<User>();
        try {
            while (userOne.next()){
                User user1 =new User();
                user1.setId(userOne.getInt("id"));
                user1.setName(userOne.getString("uname"));
                user1.setPassword(userOne.getString("upassword"));
                user1.setAge(userOne.getInt("age"));
                user1.setSex(userOne.getInt("sex"));
                userList.add(user1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public boolean delUserById(int id) {
        int i = userDao.delUser(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean insertUser(User user) {
        int i = userDao.addUser(user);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateUser(User user) {
        int i = userDao.editUser(user);
        if(i>0){
            return true;
        }
        return false;
    }
}
